package org.study.mybatis.plus.dynamic.boot.service;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.study.mybatis.plus.dynamic.boot.model.Ds1User;
import org.study.mybatis.plus.dynamic.boot.model.Ds2User;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2022-07-12
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private Ds1UserService ds1UserService;
    @Autowired
    private Ds2UserService ds2UserService;

    @DSTransactional
    @Override
    public void truncateTable() {
        ds1UserService.truncateTable();
        ds2UserService.truncateTable();
    }

    @DSTransactional
    @Override
    public void testCommit() {
        Ds1User ds1User = Ds1User.builder().name("AAA").age(20).build();
        ds1UserService.save(ds1User);

        Ds2User ds2User = Ds2User.builder().name("BBB").age(20).build();
        ds2UserService.save(ds2User);
    }

    @DSTransactional
    @Override
    public void testRollback01(boolean ex) {
        Ds1User ds1User = Ds1User.builder().name("AAA").age(20).build();
        ds1UserService.save(ds1User);
        if (ex) {
            int result = 100 / 0;
        }
        Ds2User ds2User = Ds2User.builder().name("BBB").age(20).build();
        ds2UserService.save(ds2User);
    }

    @DSTransactional
    @Override
    public void testRollback02(boolean ex) {
        Ds1User ds1User = Ds1User.builder().name("AAA").age(20).build();
        ds1UserService.save(ds1User);

        Ds2User ds2User = Ds2User.builder().name("BBB").age(20).build();
        ds2UserService.save(ds2User);
        if (ex) {
            int result = 100 / 0;
        }
    }
}
